package com.uboxol.example.tengu.api;

import com.alibaba.fastjson.JSONObject;
import com.uboxol.cloud.lang.TenguContext;
import com.uboxol.cloud.tengu.common.server.annotation.TenguApi;
import com.uboxol.cloud.tengu.common.server.bean.OpenContext;
import com.uboxol.cloud.tengu.common.server.bean.ServiceContext;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * 获取开放平台相关信息
 *
 * @author liyunde
 * @since 2022/5/19 14:38
 */
@Api(tags = "开放平台信息接口示例")
@TenguApi(permission = true)
@Slf4j
@ResponseBody
@RestController
@RequestMapping("api/platform")
public class PlatformController {

    @PostMapping(value = "context")
    @ApiOperation(value = "获取平台信息", notes = "平台信息与请求参数")
    public Object context(TenguContext context) {
        logger.info("当前账号绑定的Customer:{}", context.getCustomer());
        logger.info("请求的账号:{}", context.getAppId());
        logger.info("请求的token:{}", context.getAppAuthToken());
        logger.info("请求的业务参数:{}", context.getBizContent());
        return context.getBizObject();
    }

    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    public static class BizData {
        String vm;
        Long node;
    }

    @PostMapping(value = "mvc/biz")
    @ApiOperation(value = "直接使用请求参数", notes = "直接使用请求参数,兼容原来的mvc接口")
    public Object biz(BizData biz) {
        //通过服务工具获取平台信息
        TenguContext context = ServiceContext.getTenguContext();
        logger.info("当前账号绑定的Customer:{}", context.getCustomer());
        logger.info("请求的账号:{}", context.getAppId());
        logger.info("请求的token:{}", context.getAppAuthToken());
        logger.info("请求的业务参数:{}", context.getBizContent());
        return biz;
    }

}
